import requests


class FeishuApi:
    def __init__(self, app_id, app_secret, chat_name):
        self.app_id = app_id
        self.app_secret = app_secret
        self.chat_name = chat_name
        self.access_token = self.get_access_token()
        self.headers = {
            "Authorization": f"Bearer {self.access_token}",
            "Content-Type": "application/json",
        }

        res = self.get_chat_list()
        self.chat_id = res.get("chat_id")

    # 获取token
    def get_access_token(self):
        data = {"app_id": self.app_id, "app_secret": self.app_secret}
        try:
            res = requests.post(
                "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/",
                json=data,
            )
            if res.status_code == 200:
                res_json = res.json()
                access_token = res_json.get("tenant_access_token")
                return access_token
        except Exception as e:
            return {"error": e}

    # 获取群列表
    def get_chat_list(self):
        params = {"page_size": 100, "page_token": ""}
        try:
            res = requests.get(
                "https://open.feishu.cn/open-apis/chat/v4/list",
                params=params,
                headers=self.headers,
            )
            if res.status_code == 200:
                res_json = res.json()
                data = res_json.get("data")
                groups = data.get("groups")
                for i in groups:
                    if i.get("name") == self.chat_name:
                        return i
        except Exception as e:
            return {"error": e}

    def send_msg(self, text):

        data = {"chat_id": self.chat_id, "msg_type": "text", "content": {"text": text}}
        try:
            res = requests.post(
                "https://open.feishu.cn/open-apis/message/v4/send/",
                headers=self.headers,
                json=data,
            )
            return res.json()
        except Exception as e:
            return {"error": e}

    def get_member_list(self):
        url = f"https://open.feishu.cn/open-apis/im/v1/chats/:{self.chat_id}/members"
        try:
            res = requests.get(url, headers=self.headers)
            return res.json()
        except Exception as e:
            raise e

    # def get_user_id(self):
    #     mobiles="18858807935"
    #     userurl = "https://open.feishu.cn/open-apis/user/v1/batch_get_id?mobiles=%s" % mobiles
    #     res = requests.get(url=userurl, headers=self.headers)
    #     response = res.json()['data']['mobile_users'][mobiles][0]['user_id']
    #     return response


if __name__ == "__main__":
    # app_id, app_secret="cli_9d48da8e48edd10c", "qQdve1PJlrzNX98yT5JiAbhtxO1NfsS2" # jarvis
    app_id, app_secret = "cli_9d48da8e48edd10c", "qQdve1PJlrzNX98yT5JiAbhtxO1NfsS2"

    chat_name = "分区与审核"
    fei = FeishuApi(app_id, app_secret, chat_name)
    print(fei.chat_id)
    # print(fei.get_member_list())
    messages = "这是给你的测试"
    # print(res)
